package com.jrx.anytxn.param.controller.product;

import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.i18n.response.TxnRespResult;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.param.dto.product.PrDelqTableReq;
import com.jrx.anytxn.param.dto.product.PrDelqTableRes;
import com.jrx.anytxn.param.plugins.DeleteLog;
import com.jrx.anytxn.param.plugins.DeleteLogHelper;
import com.jrx.anytxn.param.service.product.IDelqTableService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import jrx.auth.filter.service.InfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

/**
 * 延滞参数controller层
 *
 * @author yf
 * date 2019/10/17 0017
 * @version V1.0
 **/
@Api(tags = "页面延滞参数API接口")
@RestController
@RequestMapping(value = "/param/delqTable")
public class DelqTableController {

    private static final Logger logger = LoggerFactory.getLogger(DelqTableController.class);

    @Autowired
    private IDelqTableService iDelqTableService;
    @Autowired
    private InfoService infoService;

    @ApiOperation(value = "根据租户ID和延滞参数ID查询延滞参数表信息", notes = "根据租户ID和延滞参数ID查询延滞参数表信息")
    @GetMapping(value = "/{tenantId}/{delqId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tenantId", value = "租户ID", required = true),
            @ApiImplicitParam(name = "delqId", value = "延滞参数表ID", required = true)
    })
    public TxnRespResult<PrDelqTableRes> getByTenIdAndDelqId(@NotNull @PathVariable String tenantId, @PathVariable String delqId) {
        TxnRespResult<PrDelqTableRes> result = new TxnRespResult<>();
        PrDelqTableRes res;
        try {
            res = iDelqTableService.findByTenIdAndDelqId(String.valueOf(infoService.getUser().getProjectId()), delqId);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "分页查询延滞参数表信息", notes = "分页查询延滞参数表信息")
    @GetMapping(value = "/pageNum/{pageNum}/pageSize/{pageSize}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true),
            @ApiImplicitParam(name = "pageSize", value = "页大小", required = true)})
    public TxnRespResult<TxnPage<PrDelqTableRes>> getAllPageByTenId(@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize) {
        TxnRespResult<TxnPage<PrDelqTableRes>> result = new TxnRespResult<>();
        TxnPage<PrDelqTableRes> response;
        try {
            String tenantId=String.valueOf(infoService.getUser().getProjectId());
            response = iDelqTableService.findAllPageByTenId(pageNum, pageSize, tenantId);
            result.getSuccess(response);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "新增延滞参数表信息", notes = "新增延滞参数表信息")
    @PostMapping("/")
    public TxnRespResult<PrDelqTableRes> createDelqTable(@RequestBody PrDelqTableReq prDelqTableReq) {
        PrDelqTableRes res;
        TxnRespResult<PrDelqTableRes> result = new TxnRespResult<>();
        try {
            prDelqTableReq.setTenantId(String.valueOf(infoService.getUser().getProjectId()));
            prDelqTableReq.setUpdateBy(infoService.getUser().getUserName());
            res = iDelqTableService.addDelqTable(prDelqTableReq);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "根据租户ID编辑延滞参数表信息", notes = "根据租户ID编辑延滞参数表信息")
    @PutMapping("/")
    public TxnRespResult<PrDelqTableRes> modifyDelqTable(@RequestBody PrDelqTableReq prDelqTableReq) {
        TxnRespResult<PrDelqTableRes> result = new TxnRespResult<>();
        PrDelqTableRes res;
        try {
            prDelqTableReq.setUpdateBy(infoService.getUser().getUserName());
            res = iDelqTableService.modifyDelqTable(prDelqTableReq);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "根据租户ID和延滞参数ID删除延滞参数表信息", notes = "根据租户ID和延滞参数ID删除延滞参数表信息")
    @DeleteMapping(value = "/{tenantId}/{delqId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tenantId", value = "租户ID", required = true),
            @ApiImplicitParam(name = "delqId", value = "延滞参数ID", required = true)
    })
    public TxnRespResult<Boolean> removeByTenIdAndDelqId(@NotNull @PathVariable String tenantId, @PathVariable String delqId) {
        TxnRespResult<Boolean> result = new TxnRespResult<>();
        Boolean flag;
        try {
            DeleteLog deleteLog = new DeleteLog(infoService.getUser().getUserName());
            DeleteLogHelper.setDelleteHistory(deleteLog);

            flag = iDelqTableService.removeByTenIdAndDelqId(String.valueOf(infoService.getUser().getProjectId()), delqId);
            result.getSuccess(flag);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        } finally {
            DeleteLogHelper.clearLocalHistory();
        }
        return result;
    }
}
